<?php
/**
 * 上传Service
 * Date: 2025/09/10
 */

namespace App\Services\Admin;

use App\Services\BaseService;
use Illuminate\Support\Facades\Storage;

class UploadService extends BaseService
{
    /**
     * 图片上传
     * @date 2025/09/10
     * @param \Illuminate\Http\UploadedFile $file
     * @return \Illuminate\Http\JsonResponse
     */
    public function image($file)
    {
        // 文件扩展名
        $ext = $file->extension();
        if (!in_array($ext, config('project.upload.image', []))) {
            return $this->failed('图片格式错误');
        }

        // 保存图片
        $filename = date('Ymd') . '/' . md5(uniqid() . rand(10000, 99999)) . '.' . $ext;
        if (!Storage::disk('upload')->put($filename, file_get_contents($file))) {
            return $this->failed('图片上传失败');
        }
        return $this->success(['url' => Storage::disk('upload')->url($filename)]);
    }
}
